package nige.sample;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_LINES;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glVertex3f;

import nige.d3.core.Position;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
 
/**
 * Basic game
 *
 * @author Name <email>
 * @version 1.0
 */
public class Game {
 
  /** Game title */
  public static final String GAME_TITLE = "My Game";
 
  /** Desired frame time */
  private static final int FRAMERATE = 60;
 
  /** Exit the game */
  private static boolean finished;
 
  /** Angle of rotating square */
  private static float angle;
 
  /**
   * Application init
   * @param args Commandline args
   */
  public static void main(String[] args) {
    boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
 
    try {
      init(fullscreen);
      run();
    } catch (Exception e) {
      e.printStackTrace(System.err);
      Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
    } finally {
      cleanup();
    }
    System.exit(0);
  }
 
  /**
   * Initialise the game
   * @throws Exception if init fails
   */
  private static void init(boolean fullscreen) throws Exception {
    // Create a fullscreen window with 1:1 orthographic 2D projection (default)
    Display.setTitle(GAME_TITLE);
    Display.setFullscreen(fullscreen);
 
    // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
    Display.setVSyncEnabled(true);
    
    
    // Create default display of 640x480
    Display.create();
    GLU.gluPerspective(45,16/9,0,100);
  }
 
  /**
   * Runs the game (the "main loop")
   */
  private static void run() {
 
    while (!finished) {
      // Always call Window.update(), all the time - it does some behind the
      // scenes work, and also displays the rendered output
      Display.update();
 
      // Check for close requests
      if (Display.isCloseRequested()) {
	finished = true;
      } 
 
      // The window is in the foreground, so we should play the game
      else if (Display.isActive()) {
        logic();
        render();
        Display.sync(FRAMERATE);
      } 
 
      // The window is not in the foreground, so we can allow other stuff to run and
      // infrequently update
      else {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        logic();
 
	// Only bother rendering if the window is visible or dirty
        if (Display.isVisible() || Display.isDirty()) {
          render();
        }
      }
    }
  }
 
  /**
   * Do any game-specific cleanup
   */
  private static void cleanup() {
    // Close the window
    Display.destroy();
  }
 
  /**
   * Do all calculations, handle input, etc.
   */
  private static void logic() {
    // Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      finished = true;
    }
	  glClear(GL_COLOR_BUFFER_BIT);
		glLoadIdentity();
		GLU.gluPerspective(45,16/9,0,100);
    Position.keyPressed();
    // Rotate the square
    angle += 2.0f % 360;
  }
  /**
   * Render the current frame
   */
  private static void render() {
    // clear the screen
    
    float size = 0.01f;
	for(int i=-10;i<10;i++) {
		for(int j=-10;j<10;j++) {
			glBegin(GL_LINES);
				glVertex3f(
						0f+i*size,
						-0.005f,
						-0.01f+size*j);
				glVertex3f(size+i*size,
						-0.005f,
						-0.01f+size*j);
				glVertex3f(0f+i*size,
						-0.005f,
						-0.01f+size*j);
				glVertex3f(0f+i*size,
						-0.005f,
						-0.01f+size+size*j);
			glEnd();
		}
	}    
  }
}